<?php
namespace app\api\model;

use think\Cache;

/** 
* Document
*/
class Image extends Base {
    
    protected $hidden =['update_time','delete_time'];



    /**
     * 根据图片id获取图片绝对地址
     *
     * @param int $id
     * @return string
     */
    public static function getImgUrl($id){
        // 读取配置信息
        $cacheKey = config('Img.cacheKey');
        $cacheExp = config('Img.cacheExp');

        if(! $id) return ''; // 减少数据库资源消耗

        $key = $cacheKey . $id;
        $url = Cache::get($key);
        if(empty($url)){
            $img = self::get($id);
            $url = self::getAbsUrl($img->url,$img->from);
            Cache::set($key,$url,$cacheExp); 
        }
        
        return $url;
    }
    
    /**
     * 获取url的绝对路径
     *
     * @param string $url
     * @param integer $from
     * @return string
     */
    public static function getAbsUrl($url,$from=1){
        $domain = config('Img.domain');

        if($from == 1){
            $url = $domain . $url;
        }elseif($from == 2) {
            $url = $url;
        }

        return $url;
    }


    public function getUrlAttr($value,$data){
        return self::getAbsUrl($value,$data['from']);
    }
}